package icbc.nav.control
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import icbc.app.AppGlobal;
	import icbc.app.control.Alert;
	
	import org.osflash.signals.Signal;
	
	public class BreadCrumbsControl extends Sprite
	{
		[Embed (source="/assets/separador.png")]
		public var separatorImage:Class;

		protected var panel:Sprite;
		public var viewWidth:uint;
		public var viewHeight:uint;
		private var navs:Array = [];
		public var onClick:Signal = new Signal(String);
		
		public function BreadCrumbsControl(navs:Array)
		{
			this.navs = navs;
			super();
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(Event.RESIZE, initMain);
			stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function initMain(evt:Event = null):void
		{
			if(stage.stageWidth>0)
				stage.removeEventListener(Event.RESIZE, initMain);
			else return;
			initContent();
		}

		private function initContent():void
		{
			panel = setBasePanel();
			addChild(panel);
			
			var itemWidth:uint = 120;
			var itemHeight:uint = 30;
			
			
			var gapx:uint = 3;
			var posx:uint = 0;
			var panelHeightHalf:uint = panel.height/2;
			
			for(var i:uint = 0; i<navs.length; i++)
			{
				var txt:String = navs[i];

				var item:BreadCrumbsItemControl = new BreadCrumbsItemControl({label:txt}, itemWidth, itemHeight);
				if(i < navs.length-1)
				{
					item.onClick.add(breadClickHandler);
				}
				panel.addChild(item);
				item.x = posx; posx += item.width + gapx;
				item.y = panelHeightHalf - itemHeight/2;
				
				if(i < (navs.length-1))
				{
					var separator:Bitmap = new separatorImage();
					//var separator:TextField = setSeparator();
					panel.addChild(separator);
					separator.x = posx; posx += separator.width+gapx;
					separator.y = panelHeightHalf - separator.height/2;
				} 
			}
		}

		private function setBasePanel():Sprite
		{
			viewWidth = AppGlobal.DEFAULT_WIDTH;
			viewHeight = 50;
			return buildPlainPanel();
		}
		
		private function buildPlainPanel():Sprite
		{
			var base:Sprite = new Sprite();
			base.graphics.beginFill(0xE6E8D8,0);
			base.graphics.drawRect(0, 0, viewWidth, viewHeight);
			base.graphics.endFill();
			return base;
		}

		private function setSeparator():TextField
		{
			var format:TextFormat = new TextFormat();
			format.font = "Courier";
			format.color = AppGlobal.RED_ICBC;
			format.size = 16;
			format.align = "center";
			format.bold = true;
			var txt:TextField = new TextField();
			txt.selectable = false;
			txt.defaultTextFormat = format;
			txt.text = ">";
			txt.autoSize = TextFieldAutoSize.CENTER;
			txt.width = 20;
			return txt;
		}
		
		private function breadClickHandler(data:Object):void
		{
			onClick.dispatch(data.label);
		}
	}
}